package com.example.findtherinbow;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class FindRainbowActivity extends AppCompatActivity {
    int iconWidth = 150; // 设置图标的宽度为100像素
    int iconHeight = 150; // 设置图标的高度为100像素
    int maxScore = 101;
    private FrameLayout flContainer;
    private TextView tvScore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_rainbow);


        flContainer = findViewById(R.id.fl_container);
        tvScore = findViewById(R.id.tv_score);
        tvScore.setText(String.format(getString(R.string.score),String.valueOf(maxScore)));

        flContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 当布局完成后，移除监听器以避免多次调用
                flContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                int containerWidth = flContainer.getWidth();
                int containerHeight = flContainer.getHeight();

                initData(containerWidth, containerHeight);
            }
        });

    }


    private void initData(int containerWidth, int containerHeight) {
        // 添加彩虹图标
        ImageView rainbowIcon = new ImageView(this);
        rainbowIcon.setImageResource(R.drawable.img_android_rainbow);
        rainbowIcon.setLayoutParams(new FrameLayout.LayoutParams(iconWidth, iconHeight));
        rainbowIcon.setX(getRandomXPosition(containerWidth));
        rainbowIcon.setY(getRandomYPosition(containerHeight));
        flContainer.addView(rainbowIcon);


        // 动态生成100个蓝色、红色和紫色图标
        for (int i = 1; i < maxScore; i++) {
            ImageView icon = new ImageView(this);
            icon.setImageResource(getRandomIconResource());
            icon.setLayoutParams(new FrameLayout.LayoutParams(iconWidth, iconHeight));
            icon.setX(getRandomXPosition(containerWidth));
            icon.setY(getRandomYPosition(containerHeight));
            flContainer.addView(icon);


            // 为图标设置点击事件监听器
            icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 当玩家点击图标时的操作
                    if (v != rainbowIcon) {
                        flContainer.removeView(v);
                        tvScore.setText(String.format(getString(R.string.score),String.valueOf(--maxScore)));
                    }
                }
            });
        }


        // 设置彩虹图标的点击事件
        rainbowIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                --maxScore;
                //找到了彩虹的操作 可以把分数带到下一个页面
                Intent intent = new Intent(FindRainbowActivity.this, PlayerActivity.class);
                intent.putExtra(PlayerActivity.PLAYER_SCORE,maxScore);
                startActivity(intent);
                finish();
            }
        });
    }

    private int getRandomIconResource() {
        int[] iconResources = {R.drawable.img_android_blue, R.drawable.img_android_green, R.drawable.img_android_purple, R.drawable.img_android_red, R.drawable.img_android_yellow};
        int randomIndex = (int) (Math.random() * iconResources.length);
        return iconResources[randomIndex];
    }

    private float getRandomXPosition(int containerWidth) {
        return (float) (Math.random() * (containerWidth - iconWidth)); // 考虑图标的宽度
    }

    private float getRandomYPosition(int containerHeight) {
        return (float) (Math.random() * (containerHeight - iconHeight)); // 考虑图标的高度
    }


}